package action.user;

import org.apache.struts2.ServletActionContext;

import security.MySecurityContextHandle;
import service.interf.security.IUser;
import action.common.MyBaseAction;

import com.opensymphony.xwork2.ModelDriven;
import common.impl.MD5Transform;

import dao.interf.IUserDAO;
import dao.pojo.User;

public class UserAction extends MyBaseAction<User> implements ModelDriven<User> {

	private static final long serialVersionUID = 1L;
	private String oldPwd;
	private IUser iUser;
	private IUserDAO userDAO;

	private String startTime;// oldpwd
	private String endTime;// newpwd

	private String nickName;
	private String email;
	private String qq;
	private String code;
	private String mobile;

	public IUser getiUser() {
		return iUser;
	}

	public void setiUser(IUser iUser) {
		this.iUser = iUser;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	private String passWord;
	private String nameEmail;

	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public String getNameEmail() {
		return nameEmail;
	}

	public void setNameEmail(String nameEmail) {
		this.nameEmail = nameEmail;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public IUserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public String getOldPwd() {
		return oldPwd;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

	public UserAction() {
		super(User.class);
	}

	public User getModel() {
		return model;
	}

	public void setIUser(IUser iUser) {
		this.iUser = iUser;
	}

	/**
	 * 修改密码
	 */
	public void changePwd() {
		String x = "";
		try {
			x = iUser.updUserPwd(MySecurityContextHandle.getUserId(), oldPwd,
					model.getPassword());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}

	/**
	 * 重设密码
	 */
	public void findUserTotal() {
		String str = "";
		try {
			str = userDAO.getAllCount().toString();
		} catch (Exception e) {
			str = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), str);
	}

	/** 后台管理 **/
	/**
	 * 获取所有用户
	 */
	public void getall() {
		String r = null;
		try {
			r = iUser.findByProperty("User", "*", "json", true, null, null,
					null, null, model.getPage(), model.getRows());
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), r);
	}

	/**
	 * 通过用户名查找所有用户记录
	 */
	public void findByUserName() {
		if (!validMe("userName"))
			return;
		String x = null;
		try {
			x = iUser.findByProperty("User", "*", "json", true,
					new String[] { "userName" }, new Object[] { model
							.getUserName() }, null, new String[] { "like" },
					model.getPage(), model.getRows());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), x);
	}

	/**
	 * 通过用户使能查找所有用户记录
	 */
	public void findByEnabled() {
		if (!validMe("enabled"))
			return;
		String x = null;
		try {
			x = iUser.findByProperty("User", "*", "json", true,
					new String[] { "enabled" }, new Object[] { model
							.getEnabled() }, null, null, model.getPage(), model
							.getRows());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), x);
	}

	/**
	 * 根据用户Ids禁用该用户
	 */
	public void useUnableByIds() {
		if (!validMe("ids")) {
			return;
		}
		String x = null;
		Boolean o = false;
		try {
			o = iUser.useUnableByIds(model.getIds());
			x = o.toString();
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}

	/**
	 * 根据用户Ids激活该用户
	 */
	public void useableByIds() {
		if (!validMe("ids")) {
			return;
		}
		String x = null;
		Boolean o = false;
		try {
			o = iUser.useableByIds(model.getIds());
			x = o.toString();
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}

	/**
	 * 添加用户
	 */
	public void add() {
		if (!validMe("userName"))
			return;
		String r = "1";
		try {
			model.setPassword(MD5Transform.EncoderByMd5(model.getUserName(),
					"UTF-8"));
			model.setEnabled((short) 0);
			iUser.addRecord(model);
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 根据ids删除用户信息
	 */
	public void delByIds() {
		if (!validMe("ids"))
			return;
		String x = null;
		Boolean o = false;
		try {
			o = iUser.delByIds("User", "userId", model.getIds());
			x = o.toString();
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}

}
